# -*- coding: utf-8 -*-
import scrapy
import random
from ..items import InfomationsItem
import re

import requests
import json

from ..consts import host_url


class BaiduinfosSpider(scrapy.Spider):
    name = 'baiduinfos'
    # allowed_domains = ['baidu.com']
    # start_urls = ['http://baidu.com/']

    real_baidu_top_urls = ['http://top.baidu.com/buzz?b=1&c=513&fr=topcategory_c513',
                           'http://top.baidu.com/buzz?b=1&c=513']

    today_baidu_top_urls = ['http://top.baidu.com/buzz?b=341&c=513&fr=topbuzz_b1_c513',
                            'http://top.baidu.com/buzz?b=341&c=513']

    seven_baidu_top_urls = ['http://top.baidu.com/buzz?b=42&c=513&fr=topbuzz_b341_c513',
                            'http://top.baidu.com/buzz?b=42&c=513']

    tieba_top_url = 'http://tieba.baidu.com/hottopic/browse/topicList?res_type=1'

    b_rank_url = 'https://www.bilibili.com/ranking'

    def start_requests(self):
        # 1百度实时榜
        yield scrapy.Request(
            url=self.real_baidu_top_urls[random.randint(0, 1)],
            callback=self.real_baidu_top
        )
        # 2百度今日榜
        yield scrapy.Request(
            url=self.today_baidu_top_urls[random.randint(0, 1)],
            callback=self.today_baidu_top
        )
        # 3百度7日榜
        yield scrapy.Request(
            url=self.seven_baidu_top_urls[random.randint(0, 1)],
            callback=self.seven_baidu_top
        )
        # 4百度贴吧热议榜
        yield scrapy.Request(
            url=self.tieba_top_url,
            callback=self.parse_tieba_top
        )

        # 8 B站榜单
        yield scrapy.Request(
            url=self.b_rank_url,
            callback=self.b_rank
        )

    def real_baidu_top(self, response):
        hot_html = response.xpath('//div[@class="mainBody"]//table//tr')
        body_items = []
        for item in hot_html:
            title = item.xpath('./td[@class="keyword"]/a[1]/text()').extract_first()
            if title is None:
                continue
            else:
                one_item = InfomationsItem()
                one_item['title'] = item.xpath('./td[@class="keyword"]/a[1]/text()').extract_first()
                one_item['link'] = item.xpath('./td[@class="tc"]/a[1]/@href').extract_first()
                one_item['desc'] = ''
                one_item['ft'] = '1'
                one_item['sub'] = '百度实时榜'
                one_item['tree_next'] = '1'
                # yield one_item
                body_items.append({'articleTitle': one_item['title'],
                                   'articleContent': one_item['desc'],
                                   'articleUrl': one_item['link']
                                   })

        self.uploadDatas('百度实时榜', 'http://top.baidu.com/buzz?b=1&c=513', body_items)

    def today_baidu_top(self, response):
        hot_html = response.xpath('//div[@class="mainBody"]//table//tr')
        body_items = []
        for item in hot_html:
            title = item.xpath('./td[@class="keyword"]/a[1]/text()').extract_first()
            if title is None:
                continue
            else:
                one_item = InfomationsItem()
                one_item['title'] = item.xpath('./td[@class="keyword"]/a[1]/text()').extract_first()
                one_item['link'] = item.xpath('./td[@class="tc"]/a[1]/@href').extract_first()
                one_item['desc'] = ''
                one_item['ft'] = '1'
                one_item['sub'] = '百度今日榜'
                one_item['tree_next'] = '1'
                # yield one_item
                body_items.append({'articleTitle': one_item['title'],
                                   'articleContent': one_item['desc'],
                                   'articleUrl': one_item['link']
                                   })

        self.uploadDatas('百度今日榜', 'http://top.baidu.com/buzz?b=341&c=513', body_items)

    def seven_baidu_top(self, response):
        hot_html = response.xpath('//div[@class="mainBody"]//table//tr')
        body_items = []
        for item in hot_html:
            title = item.xpath('./td[@class="keyword"]/a[1]/text()').extract_first()
            if title is None:
                continue
            else:
                one_item = InfomationsItem()
                one_item['title'] = item.xpath('./td[@class="keyword"]/a[1]/text()').extract_first()
                one_item['link'] = item.xpath('./td[@class="tc"]/a[1]/@href').extract_first()
                one_item['desc'] = ''
                one_item['ft'] = '1'
                one_item['sub'] = '百度七日榜'
                one_item['tree_next'] = '1'
                # yield one_item
                body_items.append({'articleTitle': one_item['title'],
                                   'articleContent': one_item['desc'],
                                   'articleUrl': one_item['link']
                                   })

        self.uploadDatas('百度七日榜', 'http://top.baidu.com/buzz?b=42&c=513', body_items)

    def parse_tieba_top(self, response):
        hot_html = response.xpath('//li[@class="topic-top-item"]')
        body_items = []
        for item in hot_html:
            one_item = InfomationsItem()
            one_item['title'] = item.xpath('.//a[contains(@class,"text")]/text()').extract_first()
            one_item['link'] = item.xpath('.//a[contains(@class,"text")]/@href').extract_first()
            one_item['desc'] = item.xpath('.//p[contains(@class,"desc")]/text()').extract_first()
            one_item['ft'] = '1'
            one_item['sub'] = '百度贴吧热搜榜'
            one_item['tree_next'] = '0'
            # yield one_item
            body_items.append({'articleTitle': one_item['title'],
                               'articleContent': one_item['desc'],
                               'articleUrl': one_item['link']
                               })

        self.uploadDatas('百度贴吧热搜榜', 'http://tieba.baidu.com/hottopic/browse/topicList?res_type=1', body_items)


    def b_rank(self, response):
        hot_html = response.xpath('//li[@class="rank-item"]')

        body_items = []
        for item in hot_html:
            one_item = InfomationsItem()
            one_item['title'] = item.xpath('.//div[contains(@class,"info")]/a/text()').extract_first()
            one_item['img'] = ''  # 取不到，因为response.text里面就没有
            one_item['link'] = item.xpath('.//div[contains(@class,"info")]/a/@href').extract_first()
            one_item['desc'] = ''
            one_item['ft'] = '1'
            one_item['sub'] = 'B站榜单'
            one_item['tree_next'] = '0'
            # yield one_item
            body_items.append({'articleTitle': one_item['title'],
                               'articleContent': one_item['title'],
                               'articleUrl': one_item['link']
                               })

        self.uploadDatas('B站榜单', 'https://www.bilibili.com/ranking', body_items)

    def uploadDatas(self, infoname, infourl, bodyitems):
        self.headers = {'Content-Type': 'application/json',
                        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64)\
                                    AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1'}
        res = requests.post(url=host_url + "/info",
                            data=json.dumps({'infoName': infoname, 'infoUrl': infourl}),
                            headers=self.headers)
        if res.status_code == 200:
            res_dic = json.loads(res.content.decode("utf-8"))
            resArtcles = requests.post(url=host_url + '/article/' + str(res_dic['infoId']), data=json.dumps(bodyitems), headers=self.headers)
            print(resArtcles)





